package cn.icare.cms.controller;

import cn.icare.cms.ItemCatService;
import cn.icare.cms.MemberAddressService;
import cn.icare.cms.dao.po.MemberAddress;
import cn.icare.cms.model.ro.AddItemCatRO;
import cn.icare.cms.model.ro.AddMemberAddressRO;
import cn.icare.cms.model.ro.EditItemCatRO;
import cn.icare.cms.model.ro.EditMemberAddressRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/personal/memberaddress")
@Api(tags = "REST会员收货地址")
public class MemberAddressController extends BaseController {
    @Resource
    private MemberAddressService memberAddressService;
    private final String FALL_STRING = "请求失败,服务繁忙!";


    @PostMapping()
    @ApiOperation(value = "新增收货地址", notes = "新增收货地址")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddMemberAddressRO ro, HttpServletRequest request) {

        Long memberId=this.getCurrentUserId(request);
        Long id = this.memberAddressService.createOne(ro,memberId);
        if (id > 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }


    @PutMapping()
    @ApiOperation(value = "更新单个收货地址", notes = "更新单个收货地址")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditMemberAddressRO ro, HttpServletRequest request) {
        Long memberId=this.getCurrentUserId(request);
        if (this.memberAddressService.updateOne(ro,memberId)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个收货地址", notes = "删除单个收货地址")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Long id, HttpServletRequest request) {
        Long memberId=this.getCurrentUserId(request);
        if (this.memberAddressService.deleteOne(id,memberId)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }


    @DeleteMapping()
    @ApiOperation(value = "删除多个收货地址", notes = "删除多个收货地址")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Long> ids, HttpServletRequest request) {
        Long memberId=this.getCurrentUserId(request);
        if (this.memberAddressService.deleteBatch(ids,memberId)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个收货地址", notes = "查询单个收货地址")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Long id,HttpServletRequest request) {
        MemberAddress model= this.memberAddressService.getOne(id);
        Long memberId=this.getCurrentUserId(request);
        if(model!=null && memberId.equals(model.getMemberid()))
        {
            return super.getRespOk(model);
        }
        else
        {
            return super.getRespFail("信息不存在或已经删除");
        }
    }


    @GetMapping()

    @ApiOperation(value = "查询会员所有收货地址", notes = "查询会员所有收货地址")
    public ResponseEntity<RespMsgVO> getList(HttpServletRequest request) {
        Long memberId=this.getCurrentUserId(request);
        return super.getRespOk(this.memberAddressService.getList(memberId));
    }


    //#region fallback methods

    //#endregion

}
